//
//  UIFont+Extension.swift
//  demo
//
//  Created by ZWJY on 2021/8/20.
//

import Foundation
import UIKit

// MARK: - 常用字体设置
public extension UIFont {

    /// 常规10号字体
    class var font10: UIFont {
        let size = adapt_size(font: 10)
        return systemFont(ofSize: size, weight: .regular)
    }
    /// 中等10号字体
    class var mediumFont10: UIFont {
        let size = adapt_size(font: 10)
        return systemFont(ofSize: size, weight: .medium)
    }
    /// 半粗体10号字体
    class var semiboldFont10: UIFont {
        let size = adapt_size(font: 10)
        return systemFont(ofSize: size, weight: .semibold)
    }
    /// 粗体10号字体
    class var boldFont10: UIFont {
        let size = adapt_size(font: 10)
        return systemFont(ofSize: size, weight: .bold)
    }
    /// 介于Bold和Black之间
    class var heavyFont10: UIFont {
        let size = adapt_size(font: 10)
        return systemFont(ofSize: size, weight: .heavy)
    }
    
    /// 常规12号字体
    class var font12: UIFont {
        let size = adapt_size(font: 12)
        return systemFont(ofSize: size, weight: .regular)
    }
    /// 中等12号字体
    class var mediumFont12: UIFont {
        let size = adapt_size(font: 12)
        return systemFont(ofSize: size, weight: .medium)
    }
    /// 半粗体12号字体
    class var semiboldFont12: UIFont {
        let size = adapt_size(font: 12)
        return systemFont(ofSize: size, weight: .semibold)
    }
    /// 粗体12号字体
    class var boldFont12: UIFont {
        let size = adapt_size(font: 12)
        return systemFont(ofSize: size, weight: .bold)
    }
    /// 介于Bold和Black之间
    class var heavyFont12: UIFont {
        let size = adapt_size(font: 12)
        return systemFont(ofSize: size, weight: .heavy)
    }
    
    /// 常规13号字体
    class var font13: UIFont {
        let size = adapt_size(font: 13)
        return systemFont(ofSize: size, weight: .regular)
    }

    /// 常规14号字体
    class var font14: UIFont {
        let size = adapt_size(font: 14)
        return systemFont(ofSize: size, weight: .regular)
    }
    /// 中等14号字体
    class var mediumFont14: UIFont {
        let size = adapt_size(font: 14)
        return systemFont(ofSize: size, weight: .medium)
    }
    /// 半粗体14号字体
    class var semiboldFont14: UIFont {
        let size = adapt_size(font: 14)
        return systemFont(ofSize: size, weight: .semibold)
    }
    /// 粗体14号字体
    class var boldFont14: UIFont {
        let size = adapt_size(font: 14)
        return systemFont(ofSize: size, weight: .bold)
    }
    /// 介于Bold和Black之间
    class var heavyFont14: UIFont {
        let size = adapt_size(font: 14)
        return systemFont(ofSize: size, weight: .heavy)
    }

    /// 常规15号字体
    class var font15: UIFont {
        let size = adapt_size(font: 15)
        return systemFont(ofSize: size, weight: .regular)
    }
    
    /// 常规15号字体
    class var boldFont15: UIFont {
        let size = adapt_size(font: 15)
        return systemFont(ofSize: size, weight: .bold)
    }

    /// 常规16号字体
    class var font16: UIFont {
        let size = adapt_size(font: 16)
        return systemFont(ofSize: size, weight: .regular)
    }
    /// 中等16号字体
    class var mediumFont16: UIFont {
        let size = adapt_size(font: 16)
        return systemFont(ofSize: size, weight: .medium)
    }
    /// 半粗体16号字体
    class var semiboldFont16: UIFont {
        let size = adapt_size(font: 16)
        return systemFont(ofSize: size, weight: .semibold)
    }
    /// 粗体16号字体
    class var boldFont16: UIFont {
        let size = adapt_size(font: 16)
        return systemFont(ofSize: size, weight: .bold)
    }
    /// 介于Bold和Black之间
    class var heavyFont16: UIFont {
        let size = adapt_size(font: 16)
        return systemFont(ofSize: size, weight: .heavy)
    }
    
    /// 常规18号字体
    class var font18: UIFont {
        let size = adapt_size(font: 18)
        return systemFont(ofSize: size, weight: .regular)
    }
    /// 中等18号字体
    class var mediumFont18: UIFont {
        let size = adapt_size(font: 18)
        return systemFont(ofSize: size, weight: .medium)
    }
    /// 半粗体18号字体
    class var semiboldFont18: UIFont {
        let size = adapt_size(font: 18)
        return systemFont(ofSize: size, weight: .semibold)
    }
    /// 粗体18号字体
    class var boldFont18: UIFont {
        let size = adapt_size(font: 18)
        return systemFont(ofSize: size, weight: .bold)
    }
    /// 介于Bold和Black之间
    class var heavyFont18: UIFont {
        let size = adapt_size(font: 18)
        return systemFont(ofSize: size, weight: .heavy)
    }

    /// 常规20号字体
    class var font20: UIFont {
        let size = adapt_size(font: 20)
        return systemFont(ofSize: size, weight: .regular)
    }
    /// 中等20号字体
    class var mediumFont20: UIFont {
        let size = adapt_size(font: 20)
        return systemFont(ofSize: size, weight: .medium)
    }
    /// 半粗体20号字体
    class var semiboldFont20: UIFont {
        let size = adapt_size(font: 20)
        return systemFont(ofSize: size, weight: .semibold)
    }
    /// 粗体20号字体
    class var boldFont20: UIFont {
        let size = adapt_size(font: 20)
        return systemFont(ofSize: size, weight: .bold)
    }
    /// 介于Bold和Black之间
    class var heavyFont20: UIFont {
        let size = adapt_size(font: 20)
        return systemFont(ofSize: size, weight: .heavy)
    }
    
    /// 粗体24号字体
    class var boldFont24: UIFont {
        let size = adapt_size(font: 24)
        return systemFont(ofSize: size, weight: .bold)
    }
    
    /// 介于Bold和Black之间
    class var heavyFont26: UIFont {
        let size = adapt_size(font: 26)
        return systemFont(ofSize: size, weight: .heavy)
    }

    /// 系统字体设置
    class func system(size: CGFloat, weight: UIFont.Weight) -> UIFont {
        return UIFont.systemFont(ofSize: size, weight: weight)
    }

    /// 粗体设置
    class func boldSystem(size: CGFloat) -> UIFont {
        return UIFont.boldSystemFont(ofSize: size)
    }

    /// 自定义字体
    class func customFont(_ name: String, _ size: CGFloat) -> UIFont{
        return UIFont.init(name: name, size: size) ?? systemFont(ofSize: size, weight: .regular)
    }
}

// TODO: - 全局字体适配规则，根据项目配置
public extension UIFont {

    /// 字体适配规则（固定大小）
    /// - Parameter size: 字体大小
    /// - Returns: 适配后的大小
    class func fixed_size(font size: CGFloat) -> CGFloat {
        if UIDevice.isiPad {
            return size + 4
        }else{
            if UIDevice.width < 375.0 { // 屏幕小的手机，4，5
                return size - 2
            }else if UIDevice.width == 375.0{ // 6, 6s，7，8，SE
                return size
            }else{ // p系列，x系列
                return size + 2
            }
        }
    }

    /// 字体适配规则（按比例适配）
    /// - Parameter size: 字体大小
    /// - Returns: 适配后的大小
    class func adapt_size(font size: CGFloat) -> CGFloat {
        if UIDevice.isiPad {
            return size + 4
        }else{
            // 保留整数
            let size = Int(UIDevice.ratio * size)
            return CGFloat(size)
        }
    }
}
